package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIoTemplate minIoTemplate;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile file) throws IOException {

        //1.将传入的文件转为输入流
        InputStream inputStream = file.getInputStream();

        //解决传入文件后缀名问题,这个方法得到的后缀名格式为image/png所以要处理
        String contentType = file.getContentType();
        if (StringUtils.isEmpty(contentType)) {
            log.warn("传入文件类型为空");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //以/分割
        String[] split = contentType.split("/");
        String type = split[1];

        //2.调用minio进行上传,得到一个图片访问地址,uploadFile()有四个参数要传入
        //1.文件前缀,一般为空""
        //2.文件名
        //3.输入流InputStream
        //4.文件后缀
        String url = minIoTemplate.uploadFile("", System.currentTimeMillis() + type, inputStream, "image");

        //3.将图片数据传入数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(UserThreadLocal.get().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setType((short) 0);
        wmMaterial.setUrl(url);

        int insertResult = wmMaterialMapper.insert(wmMaterial);
        //1:成功；2:失败
        if (insertResult < 1) {
            log.warn("图片数据传入失败");
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }


        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public PageResponseResult findList(WmMaterialDto dto) {

        //1.入参判空
        if (dto == null) {
            return new PageResponseResult(0,0,0);
        }

        //2.条件查询,只查询dto.getIsCollection() == 1,已收藏的
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection() == 1,WmMaterial::getIsCollection,dto.getIsCollection());

        Integer currentPage = dto.getPage();
        Integer pageSize = dto.getSize();
        //根据传入数据构建一个查询条件page
        Page<WmMaterial> page = new Page<>(currentPage,pageSize);
        Page pageResult = wmMaterialMapper.selectPage(page, wrapper);

        //PageResponseResult需要三个条件,currentPage,pageSize,总条数(Integer)
        //所以需要得到查询得到结果的总条数(long类型),用引用类型Long接收再转为Integer类型
        Long total = pageResult.getTotal();

        PageResponseResult pageResponseResult = new PageResponseResult(currentPage,pageSize,total.intValue());
        //传入数据
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }

    //todo 还有day-08六个接口

}
